package com.tritrees.easygridview.widget;

import java.util.HashMap;
import java.util.List;

import com.winway.sixbook.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ImageAdapter extends BaseAdapter
{

	private static final String TAG = "ImageAdapter";

	/** 自定义图片的宽高 */
	protected float imageWidth, imageHeight;

	public float getImageWidth()
	{
		return imageWidth;
	}

	public void setImageWidth(float imageWidth)
	{
		this.imageWidth = imageWidth;
	}

	public float getImageHeight()
	{
		return imageHeight;
	}

	public void setImageHeight(float imageHeight)
	{
		this.imageHeight = imageHeight;
	}

	protected List<HashMap<String, Object>> data;

	public void setData(List<HashMap<String, Object>> data)
	{
		this.data = data;
	}

	private Context context;
	protected int blankItemCount;// 空白子项的个数
	protected boolean isRemoved = false;// 是否点击了删除图标

	private boolean isWithText = true;// 图片下面是否带文字

	/**
	 * 设置空白区域的个数
	 * 
	 * @param blankItemCount
	 */
	public void setBlankItemCount(int blankItemCount)
	{
		this.blankItemCount = blankItemCount;
	}

	/**
	 * 设置是否为删除状态
	 * 
	 * @param isRemoved
	 */
	public void setRemoved(boolean isRemoved)
	{
		this.isRemoved = isRemoved;
	}

	public ImageAdapter(Context context)
	{
		this(context, false);
	}

	public ImageAdapter(Context context, boolean isWithText)
	{
		this.isWithText = isWithText;
		this.context = context;
	}

	@Override
	public int getCount()
	{
		return data.size();
	}

	@Override
	public Object getItem(int position)
	{
		return data.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent)
	{
		ViewHolder viewHolder = null;
		if (convertView == null)
		{
			
			
			viewHolder = new ViewHolder();
			convertView = View.inflate(context, R.layout.imageadapter_item,
					null);
			viewHolder.iv_image = (ImageView) convertView
					.findViewById(R.id.iv_image);
			if (imageWidth != 0 && imageHeight != 0)
			{
				LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewHolder.iv_image
						.getLayoutParams();
				Log.e(TAG, "params.width======>" + params.width
						+ "params.height=====>" + params.height);
				params.width = (int) imageWidth;
				params.height = (int) imageHeight;
				Log.e(TAG, "params.width2======>" + params.width
						+ "params.height2=====>" + params.height);
				viewHolder.iv_image.setLayoutParams(params);
			}

			viewHolder.tv_text = (TextView) convertView
					.findViewById(R.id.tv_text);
			
			
			BadgeView bd=new BadgeView(context, viewHolder.iv_image);
//			String name = (String) data.get(position).get(EasyGridView.TEXT);	
//						if (name != null && !"".equals(name))
//			{
//				viewHolder.badgeView.setText(name);
//	
//			}else{
//				
//				bd.hide();
//			}
			
						bd.setBadgePosition(BadgeView.POSITION_BOTTOM_LEFT);
			bd.setBadgeMargin(0);
			bd.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
			
			viewHolder.badgeView = bd;
			convertView.setTag(viewHolder);
		} else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.iv_image.setImageBitmap((Bitmap) data.get(position).get(
				EasyGridView.IMAGE));
		viewHolder.tv_text.setVisibility(View.VISIBLE);
	if (isWithText)
	{
		
		String name = (String) data.get(position).get(EasyGridView.TEXT);
		if (name != null && !"".equals(name))
			{
			
			Log.v(TAG, name);
				viewHolder.tv_text.setText(name);
	
			}else{
				viewHolder.tv_text.setVisibility(View.GONE);
			}
		} else
		{
			viewHolder.tv_text.setVisibility(View.GONE);
		}

		if (isRemoved && position < data.size() - blankItemCount)
		{
			
					if(isRemoved){
				viewHolder.badgeView.setText("-");// 设置删除的头标
				viewHolder.badgeView.setBadgeMargin(0, 0);
				viewHolder.badgeView
				.setBadgePosition(BadgeView.POSITION_TOP_LEFT);
			}
			viewHolder.badgeView.show();
//			viewHolder.badgeView.setText("-");// 设置删除的头标
//			if (!viewHolder.badgeView.isShown())
//			{
//				viewHolder.badgeView.setBadgeMargin(0, 0);
//				viewHolder.badgeView
//						.setBadgePosition(BadgeView.POSITION_TOP_LEFT);
//				viewHolder.badgeView.show();
//			}
		} else if (!isRemoved && position < data.size() - blankItemCount)
		{
//			if (viewHolder.badgeView.isShown())
//			{
//				viewHolder.badgeView.hide();
//			}
			viewHolder.badgeView.show();
		}
		
		else if ( position >= data.size()-2-blankItemCount )
		{
			
			
			
//			if (viewHolder.badgeView.isShown())
//			{
//				viewHolder.badgeView.hide();
//			}
			viewHolder.badgeView.hide();
		}
		
		// 空白区域badgeview的控制
		if (position < data.size() && position >= data.size() - blankItemCount)
		{
			viewHolder.badgeView.hide();
			
		}
		
		Log.v(TAG,position+"---"+data.size()+"=="+blankItemCount );
		
	
		return convertView;

	}

	class ViewHolder
	{
		ImageView iv_image;
		TextView tv_text;
		
		TextView tv_text2;
		BadgeView badgeView;
	}
}
